package dashboard

import (
	"github.com/gin-gonic/gin"
	"github.com/nick/vdamtools/apps/monitor"
	"net/http"
)

func getCpus(c *gin.Context) {
	overall, logicalUtilizations, err := monitor.GetCPUUtilizations()
	if err == nil {
		c.JSON(http.StatusOK, gin.H{
			"status":  "OK",
			"message": "return success",
			"data": gin.H{
				"overall_cpu_usage":   overall,
				"per_core_cpu_usages": logicalUtilizations,
			},
		})
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
	}
}

func getMem(c *gin.Context) {
	v, err := monitor.GetMemory()
	if err == nil {
		c.JSON(http.StatusOK, gin.H{
			"status":  "OK",
			"message": "return success",
			"data": gin.H{
				"usage_percent": v.UsedPercent,
				"total":         float64(v.Total) / (1024 * 1024 * 1024),
				"free":          float64(v.Free) / (1024 * 1024 * 1024),
				"used":          float64(v.Used) / (1024 * 1024 * 1024),
			},
		})
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
	}
}
